package pc.util;

import com.idautomation.linear.*;
import com.idautomation.linear.encoder.*;

import java.sql.*;
import java.text.*;
import java.util.*;
import java.math.*;
import java.io.*;

public class Utils
{
  public static final java.sql.Date nullDate = new java.sql.Date(0);
  public static final java.sql.Time nullTime = new java.sql.Time(0);
  //
  static private final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yy");
  static private final SimpleDateFormat dateFormat_ = new SimpleDateFormat("yyyy-MM-dd");
  static private final SimpleDateFormat timeFormat_ = new SimpleDateFormat("HHmmss");
  static private final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
  static private final SimpleDateFormat date_timeFormat = new SimpleDateFormat("dd-MMM-yy HH:mm");
  static private final String[] Months = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",null};

  public String Goto(String page)
  {
    return "<script language=" + '"' + "javascript" + '"' + ">" +
        "document.location.href = " + '"' + page + '"' + "</script>";
  }

  public static String Bound(String s,char ch)
  {
    return ch  + s + ch;
  }

  public static String PadLeft(String s,int len, char ch)
  {
    String st = "";
    s = s.trim();
    for (int i = 0; i < len-s.length(); i++) {
      st = "0" + st;
    }
    return st + s;
  }

  public static String dateToStr(java.util.Date date)
  {
    if (date == null) return " ";
    return dateFormat.format(date);
  }

  public static String dateToStr(java.sql.Date date)
  {
    if (date == null) return " ";
    if (date.compareTo(nullDate) == 0) return "";
    return dateFormat.format(date);
  }

  public static String timeToStr(Time time)
  {
    if (time == null) return "     ";
    return timeFormat.format(time);
  }

  public static String timeToStr(java.util.Date time)
  {
    if (time == null) return "     ";
    return timeFormat.format(time);
  }

  public static String date_timeToStr(java.util.Date date)
  {
    if (date == null) return "";
    return date_timeFormat.format(date);
  }

  private static int getMonth(String str)
  {
    if (str == null) return 0;
    if (str.length() != 9) return 0;
    String mon = str.substring(3,6);
    for (int i = 0; Months[i] != null; i++ ) {
      if (Months[i].equals(mon)) return i + 1;
    }
    return 0;
  }

  public static java.sql.Time strToTime(String str)
  {
    if (str == null) return null;
    if (str.length() == 5) str += ":00";
    if (str.length() != 8) return null;
    if (str.indexOf(':') != 2 || str.lastIndexOf(':') != 5) return null;
    return java.sql.Time.valueOf(str);
  }

  public static java.sql.Date strToDate(String str)
  {
    if (str == null) return null;
    if (str.length() != 9) return null;
    if (!isDateStr(str)) return null;
    String yy = "20" + str.substring(7,9);
    String mm = formatNumber(getMonth(str),2);
    String dd = str.substring(0,2);
    return java.sql.Date.valueOf(yy + "-" + mm + "-" + dd);
  }

  public static boolean isDateStr(String str)
  {
    return (getMonth(str) != 0);
  }

  public  static String formatNumber(int number,int len)
  {
    String pat = "";
    for (int i = 0; i < len; i++) {
      pat = pat + "0";
    }
    DecimalFormat numberFormat = new DecimalFormat(pat);
    return numberFormat.format(number);
  }

  public  static String formatNumber(long number,int len)
  {
    String pat = "";
    for (int i = 0; i < len; i++) {
      pat = pat + "0";
    }
    DecimalFormat numberFormat = new DecimalFormat(pat);
    return numberFormat.format(number);
  }

  private static DecimalFormat setNumberFormat(int dec,boolean grouped)
  {
    DecimalFormat numberFormat = new DecimalFormat();
    numberFormat.setDecimalSeparatorAlwaysShown(false);
    numberFormat.setGroupingSize(3);
    numberFormat.setGroupingUsed(grouped);
    numberFormat.setMaximumFractionDigits(dec);
    numberFormat.setMinimumFractionDigits(dec);
    return numberFormat;
  }

  private static DecimalFormat setNumberFormat(int dec)
  {
    return setNumberFormat(dec,true);
  }


  public static float strToFloat(String s)
  {
    if (s == null) return 0;
    s = s.trim();
    if (s.length() == 0) return 0;
    if (s.lastIndexOf('.') != s.indexOf('.')) return 0;
    s = s.replaceAll(",","");
    for (int i = 0; i < s.length(); i++) {
      //if ((s.charAt(i) < '0' || s.charAt(i) > '9') && (s.charAt(i) != '.') && (s.charAt(i) != '-')) return 0;
    }
    float value = 0;
    try {
      value = Float.parseFloat(s);
    } catch (Exception e) {
      System.out.println("pc.util.Utils.strToFloat : "+ e.getMessage());
      //.printStackTrace();
      value = 0;
    }
    return value;
  }

  public static int strToInt(String s)
  {
    return (int)strToFloat(s);
  }

  public static long strToLong(String s)
  {
    return (long)strToFloat(s);
  }

  public static double strToDouble(String s)
  {
    return (double)strToFloat(s);
  }

  public static String numberToStr(double number,int dec)
  {
    return setNumberFormat(dec).format(number);
  }

   public static String numberToStr(double number)
   {
     return setNumberFormat(4).format(number);
   }

   public static String numberToStr(BigDecimal number)
   {
     return setNumberFormat(4).format(number);
   }

   public static String numberToStr(BigDecimal number,int dec)
   {
     return setNumberFormat(dec).format(number);
   }

   public static String numberToStr(float number,int dec,boolean grouped)
   {
     return setNumberFormat(dec,grouped).format(number);
   }

   public static String numberToStr(float number,boolean grouped)
   {
     return setNumberFormat(4,grouped).format(number);
   }

   public static String numberToStr(float number)
   {
     return setNumberFormat(4).format(number);
   }

   public static String numberToStr(float number,int dec)
   {
     return setNumberFormat(dec).format(number);
   }

   public static java.sql.Time now()
   {
     //java.util.Date time = new java.util.Date();
     return new java.sql.Time(new java.util.Date().getTime());
   }

   public static java.sql.Date today()
   {
     return new java.sql.Date(new java.util.Date().getTime());
   }

   public static java.sql.Date nextDate(java.sql.Date date)
   {
     Calendar cal = Calendar.getInstance();
     cal.setTime(date);
     cal.add(Calendar.DATE,1);
     return new java.sql.Date(cal.getTime().getTime());
   }

   public static java.sql.Date previousDate(java.sql.Date date)
   {
     Calendar cal = Calendar.getInstance();
     cal.setTime(date);
     cal.add(Calendar.DATE,-1);
     return new java.sql.Date(cal.getTime().getTime());
   }

   public static java.sql.Date nextMonth(java.sql.Date date)
   {
     Calendar cal = Calendar.getInstance();
     cal.setTime(date);
     cal.add(Calendar.MONTH,1);
     return new java.sql.Date(cal.getTime().getTime());
   }

   public static java.sql.Date firstDateOfMonth(java.sql.Date date)
   {
     return java.sql.Date.valueOf(date.toString().substring(0,8) + "01");
   }

   public static java.sql.Date lastDateOfMonth(java.sql.Date date)
   {
     date = nextMonth(date);
     date = firstDateOfMonth(date);
     return previousDate(date);
   }

   public static int getDateYear(java.sql.Date date)
   {
     Calendar cal = Calendar.getInstance();
     cal.setTime(date);
     return cal.get(Calendar.YEAR);
   }

   public static int getDateMonth(java.sql.Date date)
   {
     Calendar cal = Calendar.getInstance();
     cal.setTime(date);
     return cal.get(Calendar.MONTH);
   }

   public static int getDateDay(java.sql.Date date)
   {
     Calendar cal = Calendar.getInstance();
     cal.setTime(date);
     return cal.get(Calendar.DATE);
   }

   public static int getCurYear()
   {
     return getDateYear(today());
   }

   public static int getCurMonth()
   {
     return getDateMonth(today()) + 1;
   }

   public static int getCurDay()
   {
     return getDateDay(today());
   }

   public static String getCurDate()
   {
     java.util.Date date = new java.util.Date();
     return dateFormat_.format(date);
   }

   public static String getCurTime()
   {
     java.util.Date time = new java.util.Date();
     return timeFormat.format(time);
   }

   public static String getCurTime_()
   {
     java.util.Date time = new java.util.Date();
     return timeFormat_.format(time);
   }

   public static String toString(java.sql.Time time)
   {
     if (time == null) return null;
     return timeFormat_.format(time);
   }

   public static String toString(java.sql.Date date)
   {
     if (date == null) return null;
     return dateFormat_.format(date);
   }

   public static String toString(java.util.Date date)
   {
     return dateFormat_.format(date);
   }

   public static boolean isNull(java.sql.Date date)
   {
     if (date == null) return true;
     return (date.compareTo(nullDate) == 0);
   }

   public static void sleep(int ms)
   {
     try {
       Thread.sleep(ms);
     }
     catch (InterruptedException e) {
       System.out.println(e.getMessage());
     }
   }

   public static boolean createFolder(String path)
   {
     File file = new File(path);
     if (!file.exists())  return file.mkdir();
     else return true;
   }


   public static boolean createBarCode(String value,String file_name)
   {
     BarCode bc = new BarCode();
     bc.barType=BarCode.CODE128;
     bc.setAutoSize(true);
     //bc.setSize(1, 1);
     bc.setDataToEncode(value);
     bc.setAutoSize(true);
     //bc.setSize(1, 1);
     //String jpeg = getAsbPath("temp","barcode_.jpeg");

     barCodeEncoder bce = new barCodeEncoder(bc,"JPEG", file_name);

     if (!bce.result) {
       System.out.println("Error creating barcode");
       return false;
     }
     return true;
   }
   
   public static String setAccountingFormat(float val,int deci,boolean grp,String cur)
   {
	   String NumFormat = numberToStr(val, deci, grp);
	   String AccFormat = "";
	   if (val==0) 
	   {
		   AccFormat = "-";
	   }
	   else
	   {
		   if (cur.compareTo("%")!=0)
		   {
			   AccFormat=cur + NumFormat;
		   }
		   else
		   {
			   AccFormat=NumFormat + cur;
		   }
	   }
		   
	  return AccFormat;
   }
   
   public static long datediff(int lsyrs,int lsmth,int lsday)
   {
	    Calendar calendar1 = Calendar.getInstance();
	    Calendar calendar2 = Calendar.getInstance();
	    calendar1.set(getCurYear(), getCurMonth(), getCurDay());
	    calendar2.set(lsyrs,lsmth+1, lsday);
	    long milliseconds1 = calendar1.getTimeInMillis();
	    long milliseconds2 = calendar2.getTimeInMillis();
	    long diff = milliseconds2 - milliseconds1;
	    long diffSeconds = diff / 1000;
	    long diffMinutes = diff / (60 * 1000);
	    long diffHours = diff / (60 * 60 * 1000);
	    long diffDays = diff / (24 * 60 * 60 * 1000);
	    return diffDays;

   }
   
   public static long datediff(int lsyrs,int lsmth,int lsday,int fsyrs,int fsmth,int fsday)
   {
	    Calendar calendar1 = Calendar.getInstance();
	    Calendar calendar2 = Calendar.getInstance();
	    calendar1.set(fsyrs, fsmth, fsday);
	    calendar2.set(lsyrs,lsmth+1, lsday);
	    long milliseconds1 = calendar1.getTimeInMillis();
	    long milliseconds2 = calendar2.getTimeInMillis();
	    long diff = milliseconds2 - milliseconds1;
	    long diffSeconds = diff / 1000;
	    long diffMinutes = diff / (60 * 1000);
	    long diffHours = diff / (60 * 60 * 1000);
	    long diffDays = diff / (24 * 60 * 60 * 1000);
	    return diffDays;

   }
}
